/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdMeshModelUploadConfirmBo;
import com.comac.ins.isfd.domain.bo.query.IsfdMeshModelCollectQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdMeshModelCollectTreeVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshModelCollectVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshModelUploadConfirmVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshModelUploadDataHandlingVo;
import com.comac.ins.isfd.service.IIsfdMeshModelCollectService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 自然网格模型库-采集
 *
 * @author hxloongs
 * @date 2025-01-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/collect/mesh")
public class IsfdMeshModelCollectController extends BaseController {

    @Autowired
    private IIsfdMeshModelCollectService isfdMeshModelCollectService;

    /**
     * 查询自然网格模型库-采集列表
     */
    @SaCheckPermission("isfd:collect:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdMeshModelCollectVo> list(@RequestBody IsfdMeshModelCollectQueryBo queryBo) {
        return isfdMeshModelCollectService.queryPageList(queryBo);
    }


    /**
     * 获取自然网格模型库-采集详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:collect:query")
    @GetMapping("/{id}")
    public R<IsfdMeshModelCollectVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(isfdMeshModelCollectService.queryById(id));
    }

    /**
     * 文件上传-数据处理
     */
    @SaCheckPermission("isfd:collect:import")
    @Log(title = "文件上传-数据处理", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/upload/dataHandling", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<IsfdMeshModelUploadDataHandlingVo> uploadDataHandling(@RequestParam("file") MultipartFile file,
                                                                   @NotEmpty(message = "所选机型编号不能为空") @RequestParam("modelNumber") String modelNumber,
                                                                   @NotEmpty(message = "所选批次编号不能为空") @RequestParam("batchNumber") String batchNumber) {
        Long uploadTime = System.currentTimeMillis();
        if (file == null || file.isEmpty()) {
            throw new BaseException("上传文件不能为空");
        }
        if(batchNumber.contains(":")){
            throw new BaseException("批次编号不能包含特殊字符”:“ ,请重新编辑再试！");
        }
        IsfdMeshModelUploadDataHandlingVo resultVo = isfdMeshModelCollectService.uploadDataHandling(file, uploadTime, modelNumber, batchNumber);
        return R.ok(resultVo);
    }

    /**
     * 文件上传-确认上传
     */
    @SaCheckPermission("isfd:collect:import")
    @Log(title = "文件上传-确认上传", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/upload/confirm")
    public R<IsfdMeshModelUploadConfirmVo> uploadConfirm(@Validated(AddGroup.class) @RequestBody IsfdMeshModelUploadConfirmBo confirmBo) {
        IsfdMeshModelUploadConfirmVo resultVo = isfdMeshModelCollectService.uploadConfirm(confirmBo);
        return R.ok(resultVo);
    }

    /**
     * 查看全部文件
     */
    @SaCheckPermission("isfd:collect:detail")
    @GetMapping("/all/files")
    public R<IsfdMeshModelCollectTreeVo> allFiles(@RequestParam("id") Long id) {
        return R.ok(isfdMeshModelCollectService.allFiles(id));
    }
}
